﻿using System;
using System.ComponentModel;
using System.Windows;
using WindowsPhoneToolbox.Util;
using AppRes = WindowsPhoneToolbox.Resources;

namespace WindowsPhoneToolbox.Controls
{
    /// <summary>
    /// Interaction logic for XapDeployControl.xaml
    /// </summary>
    public partial class XapDeployControl
    {
        private XapFile _xap;
        private static bool IsInDesignModeStatic
        {
            get
            {
                return
                    (bool)
                    DependencyPropertyDescriptor.FromProperty(DesignerProperties.IsInDesignModeProperty, typeof(FrameworkElement)).Metadata.DefaultValue;
            }
        }

        public static readonly RoutedEvent UploadEvent =
            EventManager.RegisterRoutedEvent("UploadCommand", RoutingStrategy.Bubble,
            typeof(RoutedEventHandler), typeof(XapDeployControl));

        public event RoutedEventHandler UploadCommand
        {
            add { AddHandler(UploadEvent, value); }
            remove { RemoveHandler(UploadEvent, value); }
        }


        public XapDeployControl()
        {
            InitializeComponent();
            Clearxap();
            if (!IsInDesignModeStatic)
            {
                fileUpload.FileFilter = AppRes.FilterXap;
                fileUpload.FileNameChanged += FileUploFileNameChanged;
            }
            _xap = null;
        }
        
        public void Clearxap()
        {
            fileUpload.FileName = string.Empty;
            MessageDeploy.Text = string.Empty;
            BntNewDeploy.IsEnabled = false;
            BntUpdDeploy.IsEnabled = false;
            BntCache.IsEnabled = true;
            fileUpload.IsEnabled = true;
            ProductIdxap.Text = string.Empty;
            Titlexap.Text = string.Empty;
            Versionxap.Text = string.Empty;
            Authorxap.Text = string.Empty;
            Genrexap.Text = string.Empty;
            expanappdata.IsExpanded = false;
            expanappdata.IsEnabled = false;
            Chklaunch.IsEnabled = true;
            _xap = null;
        }

        private void FileUploFileNameChanged(object sender, RoutedEventArgs e)
        {
            MessageDeploy.Text = string.Empty;
            BntNewDeploy.IsEnabled = false;
            BntUpdDeploy.IsEnabled = false;
            if (string.IsNullOrEmpty(fileUpload.FileName.Trim()))
            {
                return;
            }
            if (!System.IO.File.Exists(fileUpload.FileName.Trim()))
            {
                MessageBox.Show(AppRes.FileNFound, AppRes.TitleWindows);
                Clearxap();
                return;
            }

            if (WPhoneDevice.CurrentDevice == null || (WPhoneDevice.CurrentDevice != null && !WPhoneDevice.CurrentDevice.IsConnected()))
            {
                Clearxap();
                MessageBox.Show(AppRes.ConnectionLost, AppRes.TitleWindows, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            try
            {
                _xap = new XapFile(fileUpload.FileName.Trim());
                if (WPhoneDevice.CurrentDevice.IsApplicationInstalled(new Guid(_xap.Guid)))
                {
                    BntUpdDeploy.IsEnabled = true;
                }
                BntNewDeploy.IsEnabled = true;
                expanappdata.IsEnabled = true;
                ProductIdxap.Text = _xap.Guid;
                Titlexap.Text = _xap.Title;
                Versionxap.Text = _xap.Version;
                Authorxap.Text = _xap.Author;
                Genrexap.Text = _xap.Genre;
                expanappdata.IsExpanded = true;
            }
            catch (Exception ex)
            {
                Clearxap();
                MessageBox.Show(AppRes.Error + ex.Message, AppRes.TitleWindows, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void BntNewDeployClick(object sender, RoutedEventArgs e)
        {
            var temp = UploadEvent;
            if (temp != null)
            {
                var arg = new UploadPhoneEvent(true, (Chklaunch.IsChecked.HasValue && Chklaunch.IsChecked.Value), fileUpload.FileName.Trim(), _xap.Guid, _xap.Title, _xap.Author, _xap.Genre, _xap.Version, _xap.Icon) { RoutedEvent = temp };
                RaiseEvent(arg);
            }

        }

        private void BntCacheClick(object sender, RoutedEventArgs e)
        {
            var inifolder = WpIsolateSaveData.FolderData();
            System.Diagnostics.Process.Start("explorer.exe", inifolder);
        }

        private void BntUpdDeployClick(object sender, RoutedEventArgs e)
        {
            var temp = UploadEvent;
            if (temp != null)
            {
                var arg = new UploadPhoneEvent(false, (Chklaunch.IsChecked.HasValue && Chklaunch.IsChecked.Value), fileUpload.FileName.Trim(), _xap.Guid, _xap.Title, _xap.Author, _xap.Genre, _xap.Version, _xap.Icon) { RoutedEvent = temp };
                RaiseEvent(arg);
            }
        }
    }
}
